var ScheduleStatus={
	NORMAL:	1,  //排班状态:正常
	STOP:2,     //排班状态:停诊
	RECOVER:3   //排班状态:停诊恢复
};
var OrderNum={
	NO_PEOPLE:0  //预约人数为0
};
var DayFlag={
	am:'0',		//早上
	pm:'1',		//下午
	night:'2',	//晚上
	redo:'3'	//再次处理字符串

};
$(function() {
	loadScheduleData();
	//$Alert.showLoading();
	$(document).on('mouseenter', '.schedulDiv_cont_list li>div', function() { //鼠标悬浮时，或者进行增加开诊，或者展示详细信息
		if ($(this).find('.n_add').length != 0) { //"n_add"是新增加开诊
			$(this).find('.n_add').show();
		} else if ($(this).find('.n_add').length == 0 && $(this).find('.z_morning').length != 0) {
			$(this).find('.z_morning').show();
		}
	}).on('mouseleave', '.schedulDiv_cont_list li>div', function() { //鼠标离开则隐藏漂浮层

		var showWdate = $('div[lang="zh-cn"]', window.parent.document);
		var hide_z_morning = $(this).find('.z_morning:hidden').length;
		if ($(this).find('.n_add').length != 0 && hide_z_morning == 1) {

			//当前是新开诊，填写信息的弹窗处于隐藏状态
			$(this).find('.z_morning').hide();
			$(this).find('.n_add').hide();
		} else if ($(this).find('.n_add').length != 0 && hide_z_morning != 1 && showWdate.is(':hidden')) {
			//当前是新开诊，填写信息的弹窗已显示，时间插件是关闭状态才执行hide，如果时间插件未关闭则不执行hide
			$(this).find('.z_morning').hide();
			$(this).find('.n_add').hide();
		} else if ($(this).find('.n_add').length == 0 && $(this).find('.z_morning').length != 0 && showWdate.is(':hidden')) {
			//当前不是新增加开诊，且时间选择插件关闭的情况下，鼠标离开div才进行hide操作；如果时间插件未被关闭则不进行hide
			$(this).find('.z_morning').hide();
		} else if ($(this).find('.n_add').length == 0 && $(this).find('.z_morning').length != 0 && showWdate.length == 0) {
			//当前不是新增加开诊，且当前不包含时间插件的，可直接进行hide
			$(this).find('.z_morning').hide();
		}

	});

	$(document).on('click', '.n_add', function() {
		$(this).next('.z_morning').show();
	});
	$(document).on('click', '.z_m_cancel', function() {
		$(this).parents('.z_morning').hide();
		$(this).parents('.z_morning').find('.editHtml').hide();
		$(this).parents('.z_morning').find('.leftHtml').show();

	});
	$(document).on('click', '.z_m_modify,.z_m_add', function() {
		$(this).parents('.z_morning').hide();
		var id = '';
		var timeBegin = $(this).parents('.z_morning').find('.startTime').val();
		var timeEnd = $(this).parents('.z_morning').find('.endTime').val();
		var limitNum = $(this).parents('.z_morning').find('.limitNum').val();
		var id = $(this).parents('.z_morning').find('.z_id').val();
		var date = $(this).parents('.z_morning').find('.dateTime').val();
		if (limitNum.length != 0) {
			modifyMess(id, date, timeBegin, timeEnd, limitNum, $(this));
		} else {
			$(this).parents('.z_morning').find('.limitNum').focus();
		}
	});

	var deleteObj = ''; //存储当前删除对象
	var stopObj = ''; //存储当前停诊对象
	var reuseObj = ''; //存储当前复诊对象
	$(document).on('click', '.z_morning .toDelete', function() { //删除操作
		deleteObj = $(this);
		var scheduleId = deleteObj.parents('.z_morning').find('.z_id').val();
		var date = deleteObj.parents('.z_morning').find('.dateTime').val();
		var orderNum = $.trim(deleteObj.parents('.z_morning').find('.fromNum').text());
		deleteMess(scheduleId, deleteObj, date, orderNum);

	});

	$(document).on('click', '.tomodify', function() {
		var fromClassName = $(this).parents('.z_morning').find('.fromNum').attr('classNames');
		var toClassName = $(this).parents('.z_morning').find('.toNum').attr('classNames');
		var fromNum = $.trim($(this).parents('.z_morning').find('.fromNum').text());
		var toNum = $.trim($(this).parents('.z_morning').find('.toNum').text());
		var z_m_time = $.trim($(this).parents('.z_morning').find('.z_m_time').text());
		var fromTime = (z_m_time.split('~'))[0];
		var toTime = (z_m_time.split('~'))[1];
		var obj = $(this).parents('.z_morning').find('.z_morning_left_right');
		editDiv(obj, fromTime, toTime, fromNum, toNum, fromClassName, toClassName);

	});

	$(document).on('click', '.showLeft', function() { //显示第一周排班情况
		dayStr2 = $('.schedulDiv_cont_list').html();
		$('.schedulDiv_week_time').html(dateStr1);
		$('.schedulDiv_cont_list').html(dayStr1);
		$('.schedulDiv_week_right').addClass('showRight');
		$('.schedulDiv_week_left').removeClass('showLeft');
	});

	$(document).on('click', '.showRight', function() { //显示第二周排班情况
		dayStr1 = $('.schedulDiv_cont_list').html();
		$('.schedulDiv_week_time').html(dateStr2);
		$('.schedulDiv_cont_list').html(dayStr2);
		$('.schedulDiv_week_left').addClass('showLeft');
		$('.schedulDiv_week_right').removeClass('showRight');
	});


});
var dateStr1 = ""; //显示第一周日期，周几
var dateStr2 = ""; //显示第二周日期，周几

var dayStr1 = ''; //显示第一周排班情况
var dayStr2 = ''; //显示第二周排班情况
var myTimeId = '0';
var moonFromTime = '08:00';
var moonToTime = '12:00';
var noonFromTime = '12:01';
var noonToTime = '18:00';
var nightFromTime = '18:01';
var nightToTime = '23:59';

function current(dates1) {
	var now = new Date(),
		str = '';
	
	now.setDate(now.getDate() + dates1);
	
	var dateStr ='';
	if (now.getDate() <= 9) {
		dateStr = "0" + now.getDate();
	} else {
		dateStr = now.getDate();
	}
	
	var monthStr = '';
	if ((now.getMonth() + 1) <= 9) {
		monthStr = "0" + (now.getMonth() + 1);
	} else {
		monthStr = now.getMonth() + 1;
	}

	str += now.getFullYear() + '-'; //获取当前年份
	str += monthStr + '-'; //获取当前月份（0——11）
	str += dateStr;

	return str;
}

function current1(dates1) {
	var d = new Date(),
		str1 = '';
	d.setDate(d.getDate() + dates1);
	/*= new Date(Date.parse(date));   /*/
	var weekday = new Array(7)
	weekday[0] = "周日"
	weekday[1] = "周一"
	weekday[2] = "周二"
	weekday[3] = "周三"
	weekday[4] = "周四"
	weekday[5] = "周五"
	weekday[6] = "周六"

	if (dates1 == 0) {
		str1 = "今天"
	} else {
		str1 = weekday[d.getDay()]; //获取当前星期
	}
	return str1;
}

function loadScheduleData() { //加载排班数据
	var data = '{';
	for (var i = 0; i <= 13; i++) {

		data += '"' + current(i) + '":' +
			'{"week":"' + current1(i) + '"},';
	};
	data += "}";
	/*data=data.substr(0,data.length-1);*/

	data = eval("(" + data + ")");
	//	data= JSON.parse(data);
	console.log(data)
	var dateArray = []; //日期数组
	var weekMessArray = []; //日期对应的值对象数组
	$.each(data, function(i, n) {
		dateArray.push(i);
		weekMessArray.push(n);
	});

	$.each(dateArray, function(i) {
		var dateMess = (dateArray[i]).split('-');
		dateMess = dateMess[1] + '月' + dateMess[2] + '日'; //控制日期以‘MM月MM日’格式显示
		if (i <= 6) {
			dateStr1 += "<li><div class='week_name'>" + weekMessArray[i].week + "</div>";
			dateStr1 += "<div class='week_date'>" + dateMess + "</div></li>"; //拼接顶部日期ul串
		} else {
			dateStr2 += "<li><div class='week_name'>" + weekMessArray[i].week + "</div>";
			dateStr2 += "<div class='week_date'>" + dateMess + "</div></li>"; //拼接顶部日期ul串
		}

		var moonStr = ''; //早上
		var noonStr = ''; //下午
		var nightStr = ''; //晚上

		moonStr = dealStr(weekMessArray[i].am, dateArray[i], DayFlag.am); //拼接某一天早上html串
		//console.log(weekMessArray[i].am,dateArray[i],DayFlag.am)
		noonStr = dealStr(weekMessArray[i].pm, dateArray[i], DayFlag.pm); //拼接某一天下午html串
		nightStr = dealStr(weekMessArray[i].night, dateArray[i], DayFlag.night); //拼接某一天晚上html串
		if (i <= 6) {
			dayStr1 += "<li>" + moonStr + noonStr + nightStr + "</li>"; //某一天的html串
		} else {
			dayStr2 += "<li>" + moonStr + noonStr + nightStr + "</li>"; //某一天的html串
		}

	});
	$('.schedulDiv_week_time').html(dateStr1);
	$('.schedulDiv_cont_list').html(dayStr1);
}

function editDiv(obj, fromTime, toTime, fromNum, toNum, fromClassName, toClassName) { //显示编辑框
	if (obj.find('.Wdatec').length == 0 && obj.prev('.editHtml').length == 0) {
		var index = $(obj).parents('.liDiv').index();
		var minTime = '';
		var maxTime = '';
		switch (index) {
			case 0:
				{
					minTime = moonFromTime;
					maxTime = moonToTime;
					break;
				}
			case 1:
				{
					minTime = noonFromTime;
					maxTime = noonToTime;
					break;
				}
			case 2:
				{
					minTime = nightFromTime;
					maxTime = nightToTime;
					break;
				}
		}

		obj.prev('.leftHtml').hide();
		floatCont = "<div class='z_morning_left editHtml'><div class='z_m_time clearfix'>" + "<!-- 时间插件1start -->" + "<div class='input-group clockpicker sfleft'>" + "<input type='text' class='form-control startTime Wdatec' id='" + (myTimeId++) + "' onblur=\"confirmTime(this)\" onfocus=\"WdatePicker({dateFmt:'HH:mm',minDate:'" + minTime + "',maxDate:'#F{$dp.$D(\\'" + (myTimeId) + "\\')||\\'" + maxTime + "\\'}'})\"  value='" + fromTime + "'>" + "</div>" + "<!-- 时间插件1end -->" + "<div class='sfleft'>&nbsp;~&nbsp;</div>" + "<!-- 时间插件1start -->" + "<div class='input-group clockpicker sfleft'>" + "<input type='text' class='form-control endTime Wdatec' id='" + (myTimeId) + "' onblur=\"confirmTime(this)\" onfocus=\"WdatePicker({dateFmt:'HH:mm',minDate:'#F{$dp.$D(\\'" + (myTimeId - 1) + "\\')}',maxDate:'" + maxTime + "'})\" value='" + toTime + "'>" + "</div>" + "<!-- 时间插件1end -->" + "</div>" + "<div class='z_m_limit clearfix'>" + "<div class='z_m_text'>限制人数</div>" + "<div class='z_m_to'> <input type='text' class='limitNum' onkeyup=\"this.value=this.value.replace(/\\D/g,'')\" onafterpaste=\"this.value=this.value.replace(/\\D/g,'')\" value='" + toNum + "'></div>" + "</div>" + "<div class='z_m_btns clearfix'>" + "<div class='z_m_save z_m_modify'>保&nbsp;存</div>" + "<div class='z_m_cancel'>取&nbsp;消</div>" + "</div>" + "</div>";
		myTimeId++;
		obj.before(floatCont);
	} else if (obj.prev('.editHtml').length != 0) {
		obj.parent().find('.leftHtml').hide();
		obj.prev('.editHtml').show();
	}
}

function confirmTime(obj) {
	var Time = $(obj).parents('.z_morning_left').find('.Wdatec');
	var val0 = $(Time).eq(0).val();
	var val1 = $(Time).eq(1).val();
	if (val0 == val1 && val0.length != 0) {
		alert('开始时间不能等于结束时间！');
		$(obj).focus();
	}
}

function dealStr(obj, date, DayFlagVal, delFlag) { //处理早上，下午，晚上字符串
	var htmlStr = '';
	var noUsedStr = 'm_to';
	var zeroNum = 'm_from_zero';
	var btnStr = '';
	var floatCont = '';
	var floatStop = '';
	var floatClass = '';

	var index = DayFlagVal;
	var minTime = '';
	var maxTime = '';
	var addClass = 'liDiv';
	switch (index) {
		case DayFlag.am:
			{
				minTime = moonFromTime;
				maxTime = moonToTime;
				break;
			}
		case DayFlag.pm:
			{
				minTime = noonFromTime;
				maxTime = noonToTime;
				break;
			}
		case DayFlag.night:
			{
				minTime = nightFromTime;
				maxTime = nightToTime;
				break;
			}
		case DayFlag.redo:
			{
				addClass = '';
				break;
			}
	}
	if (delFlag == DayFlag.am) {
		minTime = moonFromTime;
		maxTime = moonToTime;
	} else if (delFlag == DayFlag.pm) {
		minTime = noonFromTime;
		maxTime = noonToTime;
	} else if (delFlag == DayFlag.night) {
		minTime = nightFromTime;
		maxTime = nightToTime;
	}
	htmlStr += "<div class='" + addClass + "'>" + "<div class='n_add'></div>" + "<div class='z_morning'>" + "<input type='hidden' class='z_id' value='0'>" + "<input type='hidden' class='dateTime' value='" + date + "'>" + "<div class='z_morning_left'>" + "<div class='z_m_time clearfix'>" + "<!-- 时间插件start -->" + "<div class='input-group clockpicker sfleft'>" + "<input type='text' class='form-control startTime Wdatec' id='" + (myTimeId++) + "' onblur=\"confirmTime(this)\" onfocus=\"WdatePicker({dateFmt:'HH:mm',minDate:'" + minTime + "',maxDate:'#F{$dp.$D(\\'" + (myTimeId) + "\\')||\\'" + maxTime + "\\'}'})\"  value='" + minTime + "'>" + "</div>" + "<!-- 时间插件end -->" + "<div class='sfleft'>&nbsp;~&nbsp;</div>" + "<!-- 时间插件start -->" + "<div class='input-group clockpicker sfleft'>" + "<input type='text' class='form-control endTime Wdatec' id='" + (myTimeId) + "' onblur=\"confirmTime(this)\" onfocus=\"WdatePicker({dateFmt:'HH:mm',minDate:'#F{$dp.$D(\\'" + (myTimeId - 1) + "\\')}',maxDate:'" + maxTime + "'})\" value='" + maxTime + "'>" + "</div>" + "<!-- 时间插件end -->" + "</div>" + "<div class='z_m_limit clearfix'>" + "<div class='z_m_text'>限制人数</div>" + "<div class='z_m_to'> <input type='text' class='limitNum' onkeyup=\"this.value=this.value.replace(/\\D/g,'')\" onafterpaste=\"this.value=this.value.replace(/\\D/g,'')\" value=''></div>" + "</div>" + "<div class='z_m_btns clearfix'>" + "<div class='z_m_save z_m_add'>保&nbsp;存</div>" + "<div class='z_m_cancel'>取&nbsp;消</div>" + "</div>" + "</div>"
	+ "</div>" + "</div>";
	myTimeId++;
	return htmlStr;
}

function modifyMess(scheduleId, scheduleDate, timeBegin, timeEnd, limitNum, obj) { //新增或修改开诊信息
	if (!scheduleId) {
		scheduleId = 0;
	}
	var html = "<div class='morning '><div class='m_time'>" + timeBegin + "~" + timeEnd + "</div><div class='m_num clearfix '><div class='m_from'>" + scheduleId + "</div><div class='m_to'>" + limitNum + "</div></div><div class='z_morning'><input type='hidden' class='z_id' value='" + scheduleId + "'><input type='hidden' class='dateTime' value='" + scheduleId + "'><div class='z_morning_left leftHtml'><div class='z_m_time'>" + timeBegin + "~" + timeEnd + "</div><div class='z_m_num clearfix'><div class='z_m_from fromNum' classNames='z_m_from'>" + scheduleId + "</div><div class='z_m_to_tz toNum' classNames='z_m_to_tz'>" + limitNum + "</div></div></div><ul class='z_morning_left_right'><li class='modify tomodify'>修改</li><li class='del toDelete'>删除</li></ul></div></div>"
	obj.parents('.liDiv').html(html);

}

function deleteMess(scheduleId, obj, scheduleDate, orderNum) { //删除开诊信息

	if (orderNum > 0) {
		var parameter = {
			index: 0,
			obj: obj
		}
		$Alert.confirm('该排班已有人预约，如果删除排班将对订单做停诊操作，是否确认删除？', toDelete, parameter);
		//confirm提示框, 提示：该排班已有人预约...
		//是
		//否
	} else if (orderNum == 0) {
		/*if(data.orderNum==0) {*/
		var parameter = {
			index: 0,
			obj: obj
		}
		$Alert.confirm('是否确认删除？', toDelete, parameter, obj);
		//confirm提示框, 提示：确认是否删除
		//是
		//否
		/*} else if(data.orderNum>0) {
			$Alert.confirm('该排班已有人预约，如果删除排班将对订单做停诊操作，是否确认删除？',toDelete,parameter);
			//confirm提示框, 提示：该排班已有人预约...
			//是
			//否
		}*/
	}
}

function toDelete(parameter) { //删除数据
	var scheduleId = parameter.scheduleId;
	var scheduleDate = parameter.scheduleDate;
	var obj = parameter.obj;
	var index = parameter.index;

	var data = {
		"retCode": "1",
		"errMsg": "删除排班成功！"
	};
	// data= eval ("(" + data + ")");

	if (data.retCode == 1) { //删除成功
		var html = dealStr('', scheduleDate, DayFlag.redo, index);
		obj.parents('.liDiv').html(html);
	} else { //删除失败

	}
}